package cn.tedu.travelsystem.review.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.base.response.StatusCode;
import cn.tedu.travelsystem.review.mapper.ReviewMapper;
import cn.tedu.travelsystem.review.pojo.dto.ReviewAddParam;
import cn.tedu.travelsystem.review.pojo.dto.ReviewDeleteParam;
import cn.tedu.travelsystem.review.pojo.dto.ReviewSelectParam;
import cn.tedu.travelsystem.review.pojo.dto.ReviewUpdateParam;
import cn.tedu.travelsystem.review.pojo.entity.Review;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/review")
@Api(tags = "11.评论模块")
public class ReviewController {
    @Autowired
    private ReviewMapper reviewMapper;

    @ApiOperation("添加评论")
    @ApiOperationSupport(order = 10)
    @PostMapping("/add")
    public JsonResult add(@Validated @RequestBody ReviewAddParam reviewAddParam) {
        try {
            // 设置当前时间
//            reviewAddParam.setReviewTime(new Date());

            log.debug("开始处理【添加评论】的请求，参数：{}", reviewAddParam);

            // 执行插入操作
            ReviewAddParam reviewAdd = new ReviewAddParam();
            BeanUtils.copyProperties(reviewAddParam, reviewAdd);
            reviewAdd.setReviewTime(new Date());
            int rows = reviewMapper.insertReview(reviewAdd);


            // 判断插入是否成功
            if (rows > 0) {
                log.debug("添加评论成功");
                return JsonResult.ok(); // 插入成功，返回成功的响应
            }

            log.debug("添加评论失败");
            return new JsonResult(StatusCode.OPERATION_FAILED); // 插入失败，返回失败的响应
        } catch (Exception e) {
            log.error("添加评论时发生错误", e);
            return new JsonResult(StatusCode.OPERATION_FAILED); // 捕获异常并返回系统错误
        }
    }

    @ApiOperation("删除评论")
    @ApiOperationSupport(order = 20)
    @PostMapping("/delete")
    public JsonResult delete(@Validated @RequestBody ReviewDeleteParam reviewDeleteParam) {
        log.debug("开始处理【删除评论】的请求，参数：{}", reviewDeleteParam);
        int rows = reviewMapper.deleteReview(reviewDeleteParam);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }

    @ApiOperation("修改评论")
    @ApiOperationSupport(order = 30)
    @PostMapping("/update")
    public JsonResult update(@Validated @RequestBody ReviewUpdateParam reviewUpdateParam) {
        log.debug("开始处理【修改评论】的请求，参数：{}", reviewUpdateParam);
        int rows = reviewMapper.updateReview(reviewUpdateParam);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @ApiOperation("查询评论")
    @ApiOperationSupport(order = 40)
    @PostMapping("/select")
    public JsonResult select(@Validated @RequestBody ReviewSelectParam reviewSelectParam) {
        log.debug("开始处理【查询评论】的请求，参数：{}", reviewSelectParam);

        List<Review> reviews = reviewMapper.selectReviewList(reviewSelectParam);

        if (reviews != null && !reviews.isEmpty()) {
            return JsonResult.ok(reviews);
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED, "未找到评论记录");
        }
    }


}
